﻿using System;
using System.Net;
using System.Threading;
using System.Runtime.InteropServices;

namespace InsuranceAdjuster.Helpers {
    public class USBInsertedEventArgs : EventArgs {
        public string Message;
    }

    public class BackgroundUSBMonitor {
        public event EventHandler USBInserted;
        public void Start() {
            while (true) {
                Thread.Sleep(500);
                String[] drives = new string[] { @"c:\", @"d:\", @"e:\", @"f:\", @"g:\", @"h:\", @"i:\" };
                foreach (String drive in drives) {
                    int code = GetDriveType(drive);
                    if (code == 2) {
                        // 2 is DRIVE_REMOVABLE
                        USBInsertedEventArgs args = new USBInsertedEventArgs();
                        args.Message = drive;
                        USBInserted(this, args);
                    }
                }
            }
        }

        // see http://msdn.microsoft.com/en-us/library/aa364939(VS.85).aspx 
        // for description of Win32's GetDriveType()
        [DllImport("kernel32.dll")]
        static extern int GetDriveType(string lpRootPathName);
    }

}
